<template>
  <div class="execution">
    <basic-container>
      <el-button type="primary" @click="handleAdd()" style="margin-bottom: 20px"
        size="small">新 增
      </el-button>
      <avue-tree-table :option="option">
<!--        <el-table-column label="名称" width="200" align="center">-->
<!--          <template slot-scope="scope">-->
<!--            <span>{{scope.row.name}}</span>-->
<!--          </template>-->
<!--        </el-table-column>-->
        <el-table-column label="图标" width="200" align="center">
          <template slot-scope="scope">
            <img height="100" width="100" v-if="scope.row.iconPath" :src="scope.row.iconPath">
          </template>
        </el-table-column>
        <el-table-column label="二维码" width="200" align="center">
          <template slot-scope="scope">
            <img height="100" width="100" v-if="scope.row.codePath" :src="scope.row.codePath">
          </template>
        </el-table-column>
        <el-table-column label="操作"
                         align="center"
                         width="200">
          <template slot-scope="scope">
            <el-button type="text"
                       @click="handleGeneratingCode(scope.row)">生成二维码</el-button>
            <el-button type="text"
                       @click="handlerCateClick(scope.row)">编辑</el-button>
            <el-button type="text"
                       @click="handleDel(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </avue-tree-table>

      <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getList"></add-or-update>
    </basic-container>
  </div>
</template>

<script>
  import AddOrUpdate from './merservicestype-add-or-update'
  import Vue from 'vue'
  import { SERVICES_TYPE_DIC } from '@/utils/util'
  import { fetchList, getObj, addObj, putObj, delObj } from '@/api/merchantservices/merservicestype'
  import {generatingCode} from '@/api/merchantservices/merservicestype'
  import { tableOption } from '@/const/crud/merchantservices/merservicestype'
  // import { mapGetters } from 'vuex'
  // import store from "@/store";
  export default {
    name: 'merservicestype',
    components: {
      AddOrUpdate
    },
    data() {
      return {
        headers: {
          'token': Vue.cookie.get('token')
        },
        addOrUpdateVisible: false,
        tableData: [],
        fileList: [],
        actionUrl:'',
        page: {
          total: 0, // 总页数
          currentPage: 1, // 当前页数
          pageSize: 20 // 每页显示多少条
        },
        tableLoading: false,
        tableOption: tableOption,
        form: {
        },
        option: {
          expandAll: false,
          columns: [
            {
              text: '名称',
              value: 'name',
              width: 200
            },
            {
              text: '类型',
              value: 'type',
              type:'dic',
              dicData: SERVICES_TYPE_DIC.service_type,
            },
            {
              text: '排序',
              value: 'sort',

            }
          ],
          data: []
        }
      }
    },
    created() {
    },
    mounted: function() {
      this.actionUrl = this.$http.adornUploadUrl()
      this.getList()
    },
    // computed: {
    //   ...mapGetters(['permissions'])
    // },
    methods: {
      getList() {
        this.$http({
          url: this.$http.adornUrl("/merchantservices/merservicestype/tree"),
          method: 'get'
        }).then(res => {
          this.option.data = res.data.data
        })

      },
      /**
       * @title 打开新增窗口
       * @detail 调用crud的handleadd方法即可
       *
       **/
      handleAdd: function(id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id)
        })
      },
      handleEdit(row, index) {
        this.fileList = []
        this.form.icon = row.icon
        if (row.iconPath) {
          this.fileList = [
            {
              url: row.iconPath
            }
          ]
        }
        this.$refs.crud.rowEdit(row, index)
      },
      handleDel(row) {
        this.$confirm('是否确认删除ID为' + row.id, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(function() {
          return delObj(row.id)
        }).then(data => {
          this.$message({
            showClose: true,
            message: '删除成功',
            type: 'success'
          })
          this.getList()
        }).catch(function(err) { })
      },
      /**
       * @title 数据更新
       * @param row 为当前的数据
       * @param index 为当前更新数据的行数
       * @param done 为表单关闭函数
       *
       **/
      handleUpdate: function(row, index, done) {
        putObj(row).then(data => {
          this.tableData.splice(index, 1, Object.assign({}, row))
          this.$message({
            showClose: true,
            message: '修改成功',
            type: 'success'
          })
          done()
          this.getList(this.page)
        })
      },
      /**
       * @title 数据添加
       * @param row 为当前的数据
       * @param done 为表单关闭函数
       *
       **/
      handleSave: function(row, done) {
        addObj(row).then(data => {
          this.tableData.push(Object.assign({}, row))
          this.$message({
            showClose: true,
            message: '添加成功',
            type: 'success'
          })
          done()
          this.getList(this.page)
        })
      },
      /**
       * 刷新回调
       */
      refreshChange() {
        this.getList(this.page)
      },
      handleUploadSuccess(res, file) {
        this.form.icon = res.data.bucketName + "-" + res.data.fileName;
      },
      handleImageRemove() {
        this.form.icon = ''
      },
      handlerCateClick (row) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(row.id)
        })
      },
      handleGeneratingCode(row, index) {
        let data = new FormData()
        data.append('typeId', row.id)
        generatingCode(data)
          .then(res => {
            this.getList(this.page, {'id': this.shopId})
          })
      }
    }
  }
</script>

<style lang="scss" scoped>
</style>
